﻿var i = 0;
$(function () {
    var fakeGetJSON = function () {
        i++;
        if ((i % 2) == 0) {
            return {
                "Divisas": [
                    { name: "Dolar Oficial", compra: 5.680, venta: 5.740, estado: 1 },
                    { name: "Dolar Blue", compra: 9.05, venta: 9.10, estado: 0 },
                    { name: "Dolar Cueva", compra: 9.200, venta: 9.250, estado: -1 },
                    { name: "Dolar Nuevo", compra: 3.200, venta: 1.250, estado: 0 }],
                "Indices": [
                    { "name": "Merval", "valor": 4640.9, "porcentaje": 3.35 },
                    { "name": "Dow Jones", "valor": 15494.78, "porcentaje": 0.77 },
                    { "name": "Bovespa", "valor": 5300.8, "porcentaje": 0.05 }]
            };
        } else
            return {
                "Divisas": [
                    { name: "Dolar Oficial", compra: 6.680, venta: 6.740, estado: 0 },
                    { name: "Dolar Blue", compra: 10.05, venta: 10.10, estado: -1 },
                    { name: "Dolar Cueva", compra: 10.200, venta: 10.250, estado: 1 }],
                "Indices": [
                    { "name": "Merval", "valor": 5640.9, "porcentaje": 4.35 },
                    { "name": "Dow Jones", "valor": 16494.78, "porcentaje": 1.77 },
                    { "name": "Bovespa", "valor": 5400.8, "porcentaje": 1.05 }]
            };
    };

    var mapping = {
        'Divisas': {
            create: function(options) {
                return new myChildModel(options.data);
            }
        },
      'Indices': {
    create: function(options) {
        return new myChildModelIndices(options.data);
    }
}
    };
    
    var myChildModel = function(data) {
        var self = this;
        ko.mapping.fromJS(data, {}, self);
        
        this.formatCompra = ko.computed(function () {
            return '$ ' + this.compra().toFixed(2);
        }, this);
        
        this.formatVenta = ko.computed(function () {
            return '$ ' + this.venta().toFixed(2);
        }, this);
    };
    
    var myChildModelIndices = function (data) {
        var self = this;
        ko.mapping.fromJS(data, {}, self);

        this.valorFormat = ko.computed(function () {
            return '$ ' + this.valor().toFixed(2);
        }, this);
        
        this.porcentajeFormat = ko.computed(function () {
            return this.porcentaje() + '%';
        }, this);

    };

    // create viewmodel with default structure so the properties are created by
    // the mapping plugin
        var vm = ko.mapping.fromJS({
        "Divisas": [
            { name: "Dolar Oficial", compra: 4.680, venta: 4.740, estado: -1 },
            { name: "Dolar Blue", compra: 8.05, venta: 8.10, estado: 1 },
            { name: "Dolar Cueva", compra: 8.200, venta: 8.250, estado: 0 }],
        "Indices": [
		{ "name": "Merval", "valor": 3640.90, "porcentaje": 2.35 },
		{ "name": "Dow Jones", "valor": 14494.78, "porcentaje": 0.57 },
		{ "name": "Bovespa", "valor": 5200.80, "porcentaje": 0.55 }]},mapping);

        ko.applyBindings(vm);
    
    function poll() {
        setTimeout(function() {
            ko.mapping.fromJS(fakeGetJSON(), {}, vm);
            poll();
        }, 3000);
    }

    poll();
});